package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.SelectBean;
import com.eqdd.yiqidian.bean.rebuild.CarListInfoBean;
import com.eqdd.yiqidian.bean.rebuild.CareerListResultBean;
import com.eqdd.yiqidian.bean.rebuild.ShareConf;
import com.eqdd.yiqidian.bean.rebuild.ShareEnterConf;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditApplyTitleBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.gongneng.car.SelectCarActivity;
import com.eqdd.yiqidian.ui.select.SelectMultiPostActivity;
import com.eqdd.yiqidian.ui.share.ShareEnterActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

public class ApplyTrainActivity extends SlimBaseMultiEditActivity {
    private RecyclerViewCustom dataBinding;

    private String msg;
    @ToSave
    ArrayList<CareerListResultBean> resultList;
    private SelectBean selectBean;
    private Bundle bundle;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "培训申请");
        initTopRightText("提交", v -> {
            if (checkParams()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.APPLY_TRAIN)
                        .params("comid", AppConstent.user.getCompanyId())
                        .params("applicantGuid", AppConstent.user.getGuid())
                        .params("applicantName", AppConstent.user.getUsername())
                        .params("depid", AppConstent.user.getDepartId())
                        .params("postid", AppConstent.user.getPostId())
                        .params("depName", AppConstent.user.getDepartment())
                        .params("postName", AppConstent.user.getPost())
                        .params("trainees", setParam(1))
                        .params("theCategory", setParam(2))
                        .params("theTheme", setParam(3))
                        .params("recoDocentGuid", selectBean == null ? "" : selectBean.getGuid())
                        .params("recoDocentName", setParam(4))
                        .params("budgetedExpense", setParam(5))
                        .params("thedateStart", setParam(6))
                        .params("thedateEnd", setParam(7))
                        .params("theDemand", setParam(8))
                        .params("theReason", setParam(9))
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    isSave = false;
                                    clearData();
                                    setResult(Config.REFRESH);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    private boolean checkParams() {
        for (int i = 0; i < slimAdapterEx.getData().size(); i++) {
            if (slimAdapterEx.getDataItem(i).isNeed()) {
                if (isParamBlank(i)) {
                    msg = "请输入" + slimAdapterEx.getDataItem(i).getTitle().replace("*", "");
                    return false;
                }
            }
        }
        return true;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 1:
                bundle = new Bundle();
                bundle.putInt(Config.COM_ID, AppConstent.user.getCompanyId());
                bundle.putInt(Config.DEPARTMENT_ID, AppConstent.user.getDepartId());
                startActivityForResult(SelectMultiPostActivity.class, bundle, Config.SELECT_MULTI_POST);
                break;
            case 4:
                ShareConf shareConf = new ShareConf().setTitle("请选择")
                        .setRightTitle("完成")
                        .setMaxNum(1)
                        .setMaxTip("最多%d");
                ShareEnterConf shareEnterConf = (ShareEnterConf) new ShareEnterConf()
                        .setFriendConf(shareConf)
                        .setQunChooseConf(shareConf)
                        .setQunListConf(shareConf)
                        .setQunMemberConf(shareConf)
                        .setStaffConf(shareConf)
                        .setShare(false)
                        .setAll(false)
                        .setTitle("请选择")
                        .setRightTitle("完成")
                        .setMaxNum(1)
                        .setMaxTip("最多%d");
                bundle = new Bundle();
                bundle.putSerializable(Config.BEAN, shareEnterConf);
                startActivityForResult(ShareEnterActivity.class, bundle, Config.SHARE_ENTER);
                break;

        }
    }

    @Override
    protected void setEditBeans() {
        datas.add(new SlimEditApplyTitleBean());
        datas.add(new SlimEditEnterBean("*培训岗位", "", 1, true, "请选择").setNeed(true));
        datas.add(new SlimEditEnterBean("*培训类别", "", 2, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("*培训主题", "", 3, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("讲师推荐", "", 4, true, "请输入"));
        datas.add(new SlimEditEnterBean("*预算费用(元)", "", 5, "请输入").addSchemes(StaticScheme.Numeric()).setNeed(true));
        datas.add(new SlimEditTimeBean("*培训开始时间", "", 6, "请输入").setType(Config.YEAR_MONTH_DAY_TYPE).setNeed(true));
        datas.add(new SlimEditTimeBean("*培训结束时间", "", 7, "请输入").setType(Config.YEAR_MONTH_DAY_TYPE).setNeed(true));
        datas.add(new SlimEditEnterBean("培训要求", "", 8, "请输入"));
        datas.add(new SlimEditEnterBean("申请理由", "", 9, "请输入"));
        datas.add(new SlimEditHintBean("审批人", "", 10));
    }

    @Override
    protected void loadNetData() {
        OkGo.<HttpResult<String>>post(HttpConfig.BASE_URL + HttpConfig.GET_LEADER)
                .params("userGuid", user.getGuid())
                .params("companyId", user.getCompanyId())
                .execute(new DialogCallBack<HttpResult<String>>(ApplyTrainActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<String>> response) {
                        HttpResult<String> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.getData().get(slimAdapterEx.getData().size() - 1).setContent(httpResult.getItems());
                            slimAdapterEx.notifyItemChanged(slimAdapterEx.getData().size() - 1);
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS) {
            switch (requestCode) {
                case Config.SELECT_MULTI_POST:

                    resultList = data.getParcelableArrayListExtra(Config.LIST);
                    StringBuilder stringBuilder = null;
                    for (CareerListResultBean careerListResultBean : resultList) {
                        if (stringBuilder == null) {
                            stringBuilder = new StringBuilder(careerListResultBean.getName());
                        } else {
                            stringBuilder.append(",").append(careerListResultBean.getName());
                        }
                    }
                    slimAdapterEx.getDataItem(1).setContent(stringBuilder.toString());
                    slimAdapterEx.notifyItemChanged(1);
                    break;
                case Config.SHARE_ENTER:
                    ArrayList<SelectBean> selectBeans = data.getParcelableArrayListExtra(Config.SELECTED_BEANS);
                    selectBean = selectBeans.get(0);
                    slimAdapterEx.getDataItem(4).setContent(selectBean.getContent());
                    slimAdapterEx.notifyItemChanged(4);
                    break;

            }
        }
    }
}
